﻿using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace PrintList
{
    [ToolboxItemAttribute(false)]
    public class PrintListPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {

            try
            {
                string siteUrl = Page.Request.QueryString["site"];
                string listId = Page.Request.QueryString["list"];
                string viewId = Page.Request.QueryString["view"];

                using (SPSite siteCollection = new SPSite(siteUrl))
                {
                    using (SPWeb site = siteCollection.OpenWeb())
                    {
                        SPList list = site.Lists[new Guid(listId)];
                        SPView view = list.Views[new Guid(viewId)];
                        writer.Write(view.RenderAsHtml());
                    }
                }
            }
            catch (Exception x)
            {
                writer.Write("<p>" + x.Message + "</p>");

            }
        }
    }
}
